package com.hsource.usercenter.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hsource.usercenter.config.youZhanYun.YouZhanYunProperties;
import com.hsource.usercenter.util.RedisUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.stereotype.Service;
import com.hsource.usercenter.mapper.UserBaseMapper;
import com.hsource.usercenter.domain.UserBase;
import com.hsource.usercenter.service.IUserBaseService;

/**
 * 用户基础Service业务层处理
 *
 * @author ruoyi
 * @date 2020-10-23
 */
@Service
public class UserBaseServiceImpl implements IUserBaseService {
    @Autowired
    private UserBaseMapper userBaseMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private YouZhanYunProperties youZhanYunProperties;


    /**
     * 查询用户基础
     *
     * @param id 用户基础ID
     * @return 用户基础
     */
    @Override
    public UserBase selectUserBaseById(String id) {
        return userBaseMapper.selectUserBaseById(id);
    }

    /**
     * 查询用户基础列表
     *
     * @param userBase 用户基础
     * @return 用户基础
     */
    @Override
    public List<UserBase> selectUserBaseList(UserBase userBase) {
        return userBaseMapper.selectUserBaseList(userBase);
    }

    /**
     * 新增用户基础
     *
     * @param userBase 用户基础
     * @return 结果
     */
    @Override
    public int insertUserBase(UserBase userBase) {
        return userBaseMapper.insertUserBase(userBase);
    }

    /**
     * 修改用户基础
     *
     * @param userBase 用户基础
     * @return 结果
     */
    @Override
    public int updateUserBase(UserBase userBase) {
        return userBaseMapper.updateUserBase(userBase);
    }

    /**
     * 删除用户基础对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserBaseByIds(String ids) {
        return userBaseMapper.deleteUserBaseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户基础信息
     *
     * @param id 用户基础ID
     * @return 结果
     */
    @Override
    public int deleteUserBaseById(String id) {
        return userBaseMapper.deleteUserBaseById(id);
    }


    /**
     * 获取有赞信息
     * @return
     */
    @Override
    public JSONObject getYZYJsonObject(){
        String key=redisUtil.keyBuilder("youzhanzyun","token");
        if(redisUtil.hasKey(key)){
            String s = redisUtil.get(key).toString();
            return JSONObject.parseObject(s);
        }else {
            String s = JSONObject.toJSONString(youZhanYunProperties);
            String post = HttpUtil.post("https://open.youzanyun.com/auth/token", s);
            JSONObject jsonObject = JSONObject.parseObject(post);
            if(jsonObject.getInteger("code")==200){
                JSONObject dataJsonn = JSONObject.parseObject(jsonObject.get("data").toString());
                redisUtil.set(key,dataJsonn);
                redisUtil.expire(key,600000);
                return dataJsonn;
            }
        }
        return null;
    }
}
